package main

import (
	"fmt"
	"os"
	"strings"

	"gitee.com/haifengat/gosopt"
	"gitee.com/haifengat/gosopt/lnx"
)

var option = true // 现货 or 期权
var broker, user, pwd, appid, authCode string

func main() {
	var tmp string
	if option {
		tmp = os.Getenv("loginOPtion")
	} else {
		tmp = os.Getenv("loginETF")
	}
	fs := strings.Split(tmp, "/")
	broker, user, pwd, appid, authCode = fs[0], fs[1], fs[2], fs[3], fs[4]
	go qTest()
	go tTest()
	select {}
}

func tTest() {
	t := lnx.NewTrade()
	t.RegOnFrontConnected(func() {
		fmt.Println("connected.")
		t.ReqLogin(user, pwd, broker, appid, authCode)
	})
	t.RegOnRspUserLogin(func(loginField *gosopt.RspUserLoginField, info *gosopt.RspInfoField) {
		fmt.Println(info.ErrorID, info.ErrorMsg)
	})
	t.RegOnFrontDisConnected(func(reason int) {
		fmt.Println("disconnected: ", reason)
	})
	if option {
		fmt.Println("期权 trade tcp://58.32.234.179:45656") // 期权
		t.ReqConnect("tcp://58.32.234.179:45656")
	} else {
		fmt.Println("现货 trade tcp://58.32.234.179:35656") // 现货
		t.ReqConnect("tcp://58.32.234.179:35656")
	}
	select {}
}

func qTest() {
	q := lnx.NewQuote()
	q.RegOnFrontConnected(func() {
		fmt.Println("connected.")
		q.ReqLogin(user, pwd, broker)
	})
	q.RegOnRspUserLogin(func(loginField *gosopt.RspUserLoginField, info *gosopt.RspInfoField) {
		fmt.Println(info.ErrorID, info.ErrorMsg)
	})
	q.RegOnFrontDisConnected(func(reason int) {
		fmt.Println("disconnected: ", reason)
	})
	q.RegOnTick(func(tick *gosopt.TickField) {
		fmt.Println(tick.LastPrice)
	})
	if option {
		fmt.Println("期权 quote tcp://58.32.234.179:45555") // 期权
		q.ReqConnect("tcp://58.32.234.179:45555")
	} else {
		fmt.Println("现货 quote tcp://58.32.234.179:35555") // 现货
		q.ReqConnect("tcp://58.32.234.179:35555")
	}
	select {}
}
